/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mini
 */
public class dataBasePesan {

    public dataBasePesan() {

    }

    public void hapusPesan(String nomorPesan) {
        try {
            String query = "delete from pesan where nomor_pesan='" + nomorPesan + "'";
            DatabaseHandler.update(query);
        } catch (SQLException ex) {
            Logger.getLogger(dataBasePesan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean savePesanToDB(int no, String judul, String isi) throws SQLException {
        GregorianCalendar dateNow = new GregorianCalendar();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        String tanggal = dateFormat.format(dateNow.getTime());


        if (isi.length() > 0) {
            String queryInsert = "INSERT INTO pesan (nomor_pesan, judul_pesan, tanggal_pesan, isi_pesan) values(" + no + ",'" + judul + "','" + tanggal + "', '" + isi + "')";
            DatabaseHandler.update(queryInsert);
            return true;
        }
        return false;

    }

    public void kirimPesan(int nomor_pesan, String no_hp) throws SQLException {

        String queryInsert = "INSERT INTO pesan_terkirim (nomor_pesan, no_hp) values(" + nomor_pesan + ",'" + no_hp + "')";
        DatabaseHandler.update(queryInsert);


    }

    public int autoGenerateNomorPesan() {
        int noPesan = 0;

        try {

            String query2 = "select to_char(max(nomor_pesan)+1) from pesan";
            @SuppressWarnings("static-access")
            ResultSet rset2 = DatabaseHandler.execute(query2);
            if (rset2.next()) {
                noPesan = Integer.parseInt(rset2.getString(1));
            }
            System.out.println(noPesan);
            return noPesan;
        } catch ( SQLException ex) {
            Logger.getLogger(dataBasePesan.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(noPesan);
            return noPesan;
        }

    }
}
